﻿// Copyright (C) 2009 Monadsphere <monadshpere@gmail.com>
//
// This file is part of Monadsphere.
//
// Monadsphere is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// Monadsphere is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with Monadsphere. If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Collections;
using System.IO;
using System.Web;

using Procurios.Public;

using monadsphere.js;

/// <summary>
/// This namespace contains classes for creating ashx handlers.
/// </summary>
/// 

namespace monadsphere.ashx {

    /// <summary>
    /// General handler for requests that passes JSON data. Inheritance of
    /// this class should avoid using local variable because IsReusable()
    /// always returns true.
    /// </summary>
    public abstract class JSHandler: IHttpHandler {

        public void ProcessRequest(HttpContext context) {

            // content-type is always "text/javascript"
            context.Response.ContentType = "text/javascript";

            // thread local context
            JSContext.context = new JSContext(context.Response.Output);

            // process by passing context and JSON data
            process(context, parseJSON(context.Request));

            // recycle remove thread local context
            JSContext.context = null;
        }

        /// <summary>
        /// Parses JSON in Hashtable using Procurios.Public.JSON.
        /// 
        /// For details of Procurios.Public.JSON, see
        /// http://
        /// </summary>
        /// <param name="req">HttpRequest</param>
        protected Hashtable parseJSON(HttpRequest req) {

            String content = new StreamReader(req.InputStream).ReadToEnd();
            Hashtable json = (Hashtable) JSON.JsonDecode(content);

            return json;
        }

        /// <summary>
        /// Calls a remote procedure.
        /// </summary>
        /// <param name="name">url exclusing context</param>
        /// <param name="data">JSON in form of JSObject</param>
        protected void remoteCall(string name, JSObject data) {
            JSContext.context.writeln("monadsphere.remoteCall('" + name + "', " + data + ")");
        }

        /// <summary>
        /// This method needs to be overrided by all implementations.
        /// </summary>
        /// <param name="context"></param>
        /// <param name="data"></param>
        public abstract void process(HttpContext context, Hashtable data);

        /// <summary>
        /// This returns always true
        /// </summary>
        public bool IsReusable {
            get {
                return true;
            }
        }
    }
}
